import uuid
from starlette.middleware.base import BaseHTTPMiddleware
from fastapi import Request
from app.dependencies import request_id

class RequestIDMiddleware(BaseHTTPMiddleware):
  async def dispatch(self, request: Request, call_next):
    request_id.set(uuid.uuid4().hex)
    response = await call_next(request)
    response.headers["X-Request-Id"] = request_id.get()
    return response
